/**
 * Created by Freeman on 2016/8/8.
 */
import {push} from 'react-router-redux'
import * as types from '../constants/ActionTypes'
import api from '../api'
import {getUserInfo} from './auth'
import {showMsg} from './other'
export const getTagList = () => {
    return {
        type: types.TAG_LIST,
        promise: api.getTagList()
    }
};
//更改options
export const changeOptions = (option) => ({type: types.CHANGE_OPTIONS, option: option});

export const getArticleList = (isAdd = true) => {
    return (dispatch, getState) => {
        const options = getState().options;
        return dispatch({
            type: types.ARTICLE_LIST,
            pageSize: options.pageSize,
            promise: api.getArticleList(options),
            isAdd: isAdd
        })
    }
};

export const getArticleDetail = (id) => {
    return (dispatch, getState) => {
        const auth = getState().auth;
        api.getFrontArticle(id).then(response =>
            ({json: response.data, status: response.statusText}))
            .then(({json, status}) => {
                let isLike = false
                let article = json.data
                if (auth.user) {
                    auth.user.likes.map(item=> {
                        if (item.toString() === article._id) {
                            isLike = true
                        }
                    })
                }
                let articleDetail = {...article, isLike: isLike};
                return dispatch({
                    type: types.ARTICLE_DETAIL_SUCCESS,
                    articleDetail: articleDetail
                })
            }).catch(error => {
            return dispatch({
                type: types.ARTICLE_DETAIL_FAILURE
            })
        })
    }
};
//切换Like
function receiveToggleLike(json) {
    return {
        type: types.TOGGLE_LIKE_SUCCESS,
        like_count: json.count,
        isLike: json.isLike
    }
}

export const toggleLike = (aid) =>{
    return (dispatch,getState) =>{
        return api.toggleLike(aid)
            .then(response => ({json: response.data, status: response.statusText}))
            .then(({json,status}) => {
                if(status !== 'OK'){
                    return dispatch({ type: types.TOGGLE_LIKE_FAILURE })
                }
                dispatch(getUserInfo());
                return dispatch(receiveToggleLike(json))
            })
            .catch(error => {
                return dispatch({ type: types.TOGGLE_LIKE_FAILURE })
            })
    }
};

export const publishArticle = (article) => {
    return (dispatch,getState) =>{
        return api.addNewArticle(article)
            .then(response => ({json: response.data, status: response.statusText}))
            .then(({json,status}) => {
                if(status !== 'OK'){
                    return dispatch(showMsg(json.data && json.data.error_msg || '发表文章失败'))
                }

                dispatch(getArticleList(false));
                dispatch(showMsg('发表文章成功','success'));
                return dispatch(push('/mine/posts'))
            })
            .catch(error => {
                return dispatch(showMsg(json.data && json.data.error_msg || '发表文章失败'))
            })
    }

};

export const removeArticle = id =>{
    return (dispatch,getState) =>{
        return api.deleteArticle(id)
            .then(response => ({json: response.data, status: response.statusText}))
            .then(({json,status}) => {
                if(status !== 'OK'){
                    return dispatch(showMsg(json.data && json.data.error_msg || '删除文章失败'))
                }

                dispatch(getArticleList(false));
                dispatch(showMsg('删除文章成功','success'));
                return dispatch(push('/mine/posts'))
            })
            .catch(error => {
                return dispatch(showMsg(json.data && json.data.error_msg || '删除文章失败'))
            })
    }
}
